$(function () {
    var pro_datagrid = $("#pro_datagrid");
    var pro_dialog = $("#pro_dialog");
    var pro_form = $("#pro_form");


    pro_datagrid.datagrid({
        url: '/product/list.do',
        fit: true,
        fitColumns: true,
        toolbar:'#tb',
        columns: [
            [
                {field: 'sn', title: '产品编码', width: 100, formatter: function (value,rows) {
                    return rows ? rows.order.sn : "";
                }},
                {field: 'saleProductName', title: '商品名', width: 100, formatter: function (value,rows) {
                    console.log(rows);
                    return rows ? rows.name : "";
                }},
                {field: 'salenumber', title: '销售数量', width: 100, formatter: function (value,rows) {
                    return rows ? rows.saleNumber : "";
                }},
                {field: 'saleamount', title: '销售总金额', width: 100, formatter: function (value,rows) {
                    return rows ? rows.saleAmount : "";
                }},
                {
                    field: 'profit', title: '毛利', width: 100, formatter: function (value,rows) {
                    return rows.saleAmount-rows.saleNumber*rows.saleNumber;
                }
                }
            ]
        ],
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        onLoadSuccess:function (data) {
            var xData = new Array();
            var yData = new Array();
            var datas = data.rows;
            for(var i in datas){
                xData[i]=datas[i].saleProductName;
                yData[i]=datas[i].saleAmount;
            }

            // 基于准备好的dom，初始化echarts图表
            var myChart = echarts.init(document.getElementById('main'));
            var option = {
                tooltip: {
                    show: true
                },
                toolbox: {
                    show : true,
                    feature : {
                        mark : {show: true},
                        dataView : {show: true, readOnly: false},
                        magicType : {show: true, type: ['line', 'bar']},
                        restore : {show: true},
                        saveAsImage : {show: true}
                    }
                },
                legend: {
                    data: ['消费金额'],
                    x:'left'
                },
                xAxis: [
                    {
                        type: 'category',
                        data: xData,
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        "name": "消费金额",
                        "type": "bar",
                        "data": yData,
                        markPoint : {
                            data : [
                                {type : 'max', name: '最大值'},
                                {type : 'min', name: '最小值'}
                            ]
                        },
                        markLine : {
                            data : [
                                {type : 'average', name: '平均值'}
                            ]
                        }
                    }
                ]
            };

            // 为echarts对象加载数据
            myChart.setOption(option);
        }
    })

    var methodObj = {
        exportXls:function () {
            window.location.href = "/saleAnalysis/exportXls.do";
        }
    }

    $("a[data-cmd]").click(function () {
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })

})

